package com.usermanage.Mapping;

import com.usermanage.beanParam.GroupBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.dataDao.JpaDao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by  on 2018/9/3
 */
@Repository("groupMapping")
public interface GroupMapping extends JpaRepository<GroupBean, Integer> {

    /**
     * 验证组是否存在
     * @param groupId
     * @param groupName
     * @param parentId
     * @return
     */
    @Query(value = "SELECT count(1) FROM u_group WHERE group_id =:groupId AND group_name =:groupName AND parent_id =:parentId",nativeQuery = true)
    int checkGroupExist(@Param("groupId") String groupId,@Param("groupName") String groupName,@Param("parentId") int parentId);

    /**
     * 根据用户id得到用户所在组列表
     * @param userId
     * @return
     */
    @Query(value = "SELECT g.* from u_group_member gm LEFT JOIN u_group g ON gm.group_id = g.id WHERE gm.user_id =:userId",nativeQuery = true)
    List<Map> getUserGroupListByUserId(@Param("userId") String userId);



}
